package com.ruoyi.project.product.sku.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.tools.util.param.DomainReplace;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.product.sku.mapper.ProductSkuMapper;
import com.ruoyi.project.product.sku.domain.ProductSku;
import com.ruoyi.project.product.sku.service.IProductSkuService;

/**
 * 商品skuService业务层处理
 * 
 * @author wyh
 * @date 2024-08-05
 */
@Service
public class ProductSkuServiceImpl implements IProductSkuService 
{
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询商品sku
     * 
     * @param id 商品sku主键
     * @return 商品sku
     */
    @Override
    public ProductSku selectProductSkuById(Long id)
    {
        return productSkuMapper.selectProductSkuById(id);
    }

    /**
     * 查询商品sku列表
     * 
     * @param productSku 商品sku
     * @return 商品sku
     */
    @Override
    public List<ProductSku> selectProductSkuList(ProductSku productSku)
    {
        return productSkuMapper.selectProductSkuList(productSku);
    }

    /**
     * 新增商品sku
     * 
     * @param productSku 商品sku
     * @return 结果
     */
    @Override
    public int insertProductSku(ProductSku productSku)
    {
        return productSkuMapper.insertProductSku(productSku);
    }

    @Override
    public int insertScProductSkuList(List<ProductSku> productSku) {
        for(ProductSku item : productSku){
            if(item.getId()!=null){
                item.setUpdateTime(DateUtils.getNowDate());
                productSkuMapper.updateProductSku(item);
            }else{
                item.setCreateTime(DateUtils.getNowDate());
                productSkuMapper.insertProductSku(item);
            }
        }
        return 1;
    }

    /**
     * 修改商品sku
     * 
     * @param productSku 商品sku
     * @return 结果
     */
    @Override
    public int updateProductSku(ProductSku productSku)
    {
        return productSkuMapper.updateProductSku(productSku);
    }

    /**
     * 批量删除商品sku
     * 
     * @param ids 需要删除的商品sku主键
     * @return 结果
     */
    @Override
    public int deleteProductSkuByIds(Long[] ids)
    {

        return productSkuMapper.deleteProductSkuByIds(ids);
    }

    /**
     * 删除商品sku信息
     * 
     * @param id 商品sku主键
     * @return 结果
     */
    @Override
    public int deleteProductSkuById(Long id)
    {
        return productSkuMapper.deleteProductSkuById(id);
    }

    @Override
    public void deleteSkuByProductId(Long productId) {
        productSkuMapper.deleteSkuByProductId(productId);
    }

    @Override
    public List<ProductSku> userSelectProductSkuList(ProductSku productSku) {
        List<ProductSku> scProductSkus = productSkuMapper.selectProductSkuList(productSku);
        for(ProductSku item: scProductSkus){
            updateSkuAttr(item);
        }
        return scProductSkus;
    }

    public ProductSku updateSkuAttr(ProductSku sku){

        String domain = configService.selectConfigByKey("resources.domain");
        String be_replace_domain = configService.selectConfigByKey("resources.replace.prefix");
        String[] beReplaceDomains = be_replace_domain.split(",");
        //封面替换
        String cover = sku.getIcon();
        if(StringUtils.isNotBlank(cover)) {
            String item = DomainReplace.replacePlaceholders(cover, beReplaceDomains, domain);
            sku.setIcon(item);
        }
        return sku;
    }


}
